package com.example.zqq.wuzichess;

import android.graphics.Point;
import android.util.Log;

import java.util.List;



/**
 * “米”字型的检查是否五子连珠 on 2017/11/6.
 */

public class CheckWinner {

    private Point point1, point2;
    private int checkModel = Constants.HORIZONTAL;

    /**
     * 判断游戏是否结束
     */
    public boolean checkFiveWinner(List<Point> points) {

        for (Point point : points) {
            int x = point.x;
            int y = point.y;

//            Log.e("Tag-checkFive-Point", point.toString());

            if (checkHorizontal(x, y, points)) {
                Log.e("Tag--1111", "checkHorizontal");
                return true;
            } else if (checkVertical(x, y, points)) {
                Log.e("Tag--1111", "checkVertical");
                return true;
            } else if (checkLeftDiagonal(x, y, points)) {
                Log.e("Tag--1111", "checkLeftDiagonal");
                return true;
            } else if (checkRightDiagonal(x, y, points)) {
                Log.e("Tag--1111", "checkRightDiagonal");
                return true;
            }
        }
        return false;
    }


    //1.横向判断
    private boolean checkHorizontal(int x, int y, List<Point> points) {
        checkModel = Constants.HORIZONTAL;
        return check(x, y, points, checkModel);
    }

    //2.纵向判断
    private boolean checkVertical(int x, int y, List<Point> points) {
        checkModel = Constants.VERTICAL;
        return check(x, y, points, checkModel);
    }

    //3.左斜线判断
    private boolean checkLeftDiagonal(int x, int y, List<Point> points) {
        checkModel = Constants.LEFT_DIAGONAL;
        return check(x, y, points, checkModel);
    }

    //4.右斜线判断
    private boolean checkRightDiagonal(int x, int y, List<Point> points) {
        checkModel = Constants.RIGHT_DIAGONAL;
        return check(x, y, points, checkModel);
    }


//     针对不同的方向，做判断
//    （check的外层其实还有一层for循环）
    private boolean check(int x, int y, List<Point> points, int checkOrientation) {
        int count = 1;

        //上半“米”子
        for (int i = 1; i < Constants.MAX_COUNT_LINE; i++) {
            switch (checkOrientation) {
                case Constants.HORIZONTAL:
                    point1 = new Point(x - i, y);       //  ←
                    break;
                case Constants.VERTICAL:
                    point1 = new Point(x, y - i);       //   ↑
                    break;
                case Constants.LEFT_DIAGONAL:
                    point1 = new Point(x - i, y - i);   //   ↖
                    break;
                case Constants.RIGHT_DIAGONAL:
                    point1 = new Point(x + i, y - i);   //   ↗
                    break;
            }
//            Log.e("Tag-points", points.toString());

            if (points.contains(point1)) {
                count++;
                Log.e("Tag-11--count", String.valueOf(count));
            } else {
                break;
            }
        }

        //下半“米”字
        for (int i = 1; i < Constants.MAX_COUNT_LINE; i++) {
            switch (checkOrientation) {
                case Constants.HORIZONTAL:
                    point2 = new Point(x + i, y);       //  →
                    break;
                case Constants.VERTICAL:
                    point2 = new Point(x, y + i);       //  ↓
                    break;
                case Constants.LEFT_DIAGONAL:           //【向右向左-应该是在一条直线上 而非看箭头】
                    point2 = new Point(x + i, y + i);   //  ↘
                    break;
                case Constants.RIGHT_DIAGONAL:
                    point2 = new Point(x - i, y + i);   //  ↙
                    break;
            }
            if (points.contains(point2)) {
                count++;
            } else {
                break;
            }
        }

        return count == Constants.MAX_COUNT_LINE;   //相当于 if 语句
    }


    /************-------------****************/
    // 针对不同的方向，做判断
    //（check的外层其实还有一层for循环）
//    private boolean check(int x, int y, List<Point> points, int checkOrientation) {
//        int count = 1;
//        for (int i = 1; i < Constants.MAX_COUNT_LINE; i++) {
//            switch (checkOrientation) {
//                case Constants.HORIZONTAL:
//                    point1 = new Point(x - i, y);       //  ←
//                    Log.e("Tag--22-HORIZONTAL", point1.toString());
//                    break;
//                case Constants.VERTICAL:
//                    point1 = new Point(x, y - i);       //   ↑
//                    Log.e("Tag--22-VERTICAL", point1.toString());
//                    break;
//                case Constants.LEFT_DIAGONAL:
//                    point1 = new Point(x - i, y + i);   //   ↙
//                    Log.e("Tag--22-LEFT_DIAGONAL", point1.toString());
//                    break;
//                case Constants.RIGHT_DIAGONAL:
//                    point1 = new Point(x + i, y + i);   //   ↘
//                    Log.e("Tag--22-RIGHT_DIAGONAL", point1.toString());
//                    break;
//            }
////            Log.e("Tag-points", points.toString());
//
//            if (points.contains(point1)) {
//                count++;
//                Log.e("Tag-count", String.valueOf(count));
//            } else {
//                break;
//            }
//        }
//
//        for (int i = 1; i < Constants.MAX_COUNT_LINE; i++) {
//            switch (checkOrientation) {
//                case Constants.HORIZONTAL:
//                    point2 = new Point(x + i, y);       //  →
//                    Log.e("Tag--33-HORIZONTAL", point2.toString());
//                    break;
//                case Constants.VERTICAL:
//                    point2 = new Point(x, y + i);       //  ↓
//                    Log.e("Tag--33-VERTICAL", point2.toString());
//                    break;
//                case Constants.LEFT_DIAGONAL:       //【向右向左-应该是在一条直线上 而非看箭头】
////                    point2 = new Point(x - i, y - i);   //  ↖ （错误）
//                    point2 = new Point(x + i, y - i);  //  ↗
//                    Log.e("Tag--33-LEFT_DIAGONAL", point2.toString());
//                    break;
//                case Constants.RIGHT_DIAGONAL:
////                    point2 = new Point(x + i, y - i);   //  ↗
//                    point2 = new Point(x - i, y - i);  //  ↖
//                    Log.e("Tag--33-RIGHT_DIAGONAL", point2.toString());
//                    break;
//            }
//            if (points.contains(point2)) {
//                count++;
//            } else {
//                break;
//            }
//        }
//
//        return count == Constants.MAX_COUNT_LINE;   //相当于 if 语句
//    }



}
